﻿using Flame.Reflection;
using Microsoft.ClearScript;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Flame.Dlr
{
    class Javascript_ClearScript_Exex : IExecutable, IStreamedOutput
    {
        ScriptEngine _engine;
        List<Assembly> _assemblies = new List<Assembly>();

        public Javascript_ClearScript_Exex()
        {
            _engine = new Microsoft.ClearScript.V8.V8ScriptEngine();
        }

        public void ClearScope(ScopeParameters scopeParameters)
        {
            _assemblies.Clear();
            scopeParameters.Apply(this);
        }

        public Result Execute(string text)
        {
            return new Result() { Data = _engine.Evaluate(text) };
        }


        public ResultAssembly AddAssembly(AssemblyWrapper res)
        {
            try
            {
                var assTypes = res.Assembly.GetTypes();
                foreach (var t in assTypes)
                    _engine.AddHostType(t.FullName.Replace('.', '_'), t);
               
            }
            catch (Exception e)
            {
                return new ResultAssembly() { Exception = e, Language = GetType(), Loaded = false };
            }
            return new ResultAssembly() { Exception = null, Language = GetType(), Loaded = true };
        }

        public void AddVariable(Variable variable)
        {
            _engine.AddHostObject(variable.Name, variable.Data);
        }

        public void Set(System.IO.Stream stream)
        {
        }
        public IIntellisense Intellisense { get { return null; } }
    }
}
